/***************************************************************************
 创建者: 华磊
 开始时间: 2020.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2020.5.8 类的具体实现代码编写

 ***************************************************************************
 *  @file x.h
 *  功能:
 *
 *                                                                         *
 ***************************************************************************/
#ifndef CANBUSCOM_H
#define CANBUSCOM_H

#include "ecatmasterbase.h"
#include "robotbase.h"



class CanbusDeviceDriver;
class CanbusMaganetic;
class CanbusRfid;
class CanbusServoDriver;
class CanIoDriver;
class CanbusBattery;
/**
 * @brief The CanbusCom class can驱动
 */
class CanbusCom: public EcatMasterBase
{
public:
    CanbusCom(QString canConfigFile, QList<QString> xmlFilelist, QList<int> deviceTypeList,
              QList<int> aliasList, QList<int> canbusIdList,
              QList<int> deviceIdList,
              int pthread_priority_in=50, int pthread_schedule_in=SCHED_RR);
    ~CanbusCom();
public:
    int getCanDebugInfo(EthercatDebugInfo& debugInfo);
    int writeTagData(int aliasIn, long long cardValueIn);
    int writeTagData(int aliasIn, QVector<unsigned char> cardValueIn);
    int getCurrentTagData(int aliasIn, long long &cardValueOut, bool &isCardDetected);
    int getCurrentTagData(int aliasIn, QVector<unsigned char> &cardValueOut, bool &isCardDetected);
    int getMaganeticSensorRawValue(int aliasIn, QVector<bool> &sensorRawValueOut);
    int getBatteryInfo_fromMotor(int aliasIn, BatteryInformation &batteryInfoOut);
    int getBatteryInfo(int aliasIn,BatteryInformation &batteryInfoOut);
    int setAlias(int node, unsigned short alias){}
    int sdoUpload(int node
                  ,int rObIndex
                  ,int rObSubIndex
                  ,LONG64 &rData
                  ,int &rDataLen);
    int sdoDownload(int node
                    , int wObIndex
                    , int wObSubIndex
                    , LONG64 wData
                    , int wDataLen);

    int getMotorVendorId(int node);
    int controllerShutDown();


protected:
    void masterBootStrap(){}
    void masterError();
    void masterInit();
    void masterPreOp();
    void masterSafeOp(){}
    void masterOp();
    void updateDomainData_sdo();
    void updateDomainData_pdo();
    void updateDeviceData();
    void updateLogicData();

    int readMotorData_sdo(RobotBase *rob);
    int readMotorDataByPdo(RobotBase *rob,int &aliasOut);
    int writeMotorData_sdo(RobotBase *rob);
    int writeMotorDataByPdo(RobotBase *rob);
    int readIoData(RobotBase *rob,int &aliasOut);
    int writeIoData(RobotBase *rob);
    int readRfidData(int &aliasOut);
    int sendRfidDataRequestCommand();
    int writeRfidData();
    int readMaganeticData(int &aliasOut);
    int readBatteryData(int &aliasOut);
    int sendMaganeticDataRequestCommand();
    int setDriverNodeProtectTime();


private:
    int initialEcatIoDidoCount();
    void realTimeSleep(long long umicoroSecond);
    int createDeviceDataExchangeThread();
    //线程回调函数
    static void *threadTask_device( void* classPtr );//not used作废
    //thread fun
    void* run_device(void*);
    void wait_period_device(void);
    int checkTimeMaxLimit();
    int sendNodeWatchDogFrame();
    int startPdoMode();//目前仅仅发送伺服的命令，ＩＯ等其它设备没有发送命令。
    int initialMotorEncoderValue();//获取编码器线数
    int downloadServoSdoConfig();
    int downloadMaganeticSdoConfig();
    int downloadRfidSdoConfig();
    int updateControlMode();
//    int initialEcatIoDidoCount();
    int initial(QList<QString> xmlFilelist, QList<int> deviceTypeList, QList<int> aliasList, QList<int> canbusIdList,
                QList<int> deviceIdList, CanbusDeviceDriver *canDriverIn);
    /**
     * @brief addMsg  , 添加消息反馈
     */
    void addMsg( int messageLevel,
                 std::string componentName,
                 std::string messageType,
                 int messageCode,
                 int robotId,
                 int parameter1 = 0,
                 int parameter2 = 0,
                 int parameter3 = 0,
                 int parameter4 = 0 );


private:
    pthread_t device_thId;
    CanbusDeviceDriver *canbusDriver;

    QMap<int,CanbusServoDriver *> motorDriverMap;
    QVector<CanbusServoDriver *>motorDriver;
    QMap<int,CanbusRfid *> rfidDriverMap;
    QVector<CanbusRfid*> rfidDriver;
    QMap<int,CanbusMaganetic *> maganeticDriverMap;
    QVector<CanbusMaganetic*> maganeticDriver;
    QMap<int,CanbusBattery *> batteryDriverMap;
    QVector<CanbusBattery*> batteryDriver;
    QMap<int,CanIoDriver *> ioDriverMap;
    QVector<CanIoDriver*> ioDriver;


    int currentDeviceId;
    int currentCanbusId;

    uint64_t wakeup_time_device ;






};

#endif // CANBUSCOM_H
